package com.jtest.controller;

import com.jtest.common.Result;
import com.jtest.entity.UserEntity;
import com.jtest.service.IUserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService userService;

    @PostMapping("/add")
    public Result addUser(@RequestBody UserEntity user){
        user.setId(null);
        userService.save(user);
        return Result.success(user.getId());
    }

    @PostMapping("/update")
    public Result updateUser(@RequestBody UserEntity user) throws Exception {
        userService.updateUser(user.getId(), user.getName(), user.getAge());
        return Result.success();
    }

    @PostMapping("/delete")
    public Result deleteUser(@RequestBody UserEntity user) throws Exception {
        userService.deleteUser(user.getId());
        return Result.success();
    }

    @PostMapping("/get")
    public Result getUser(@RequestBody UserEntity user) throws Exception {
        return Result.success(userService.getUser(user.getId()));
    }
}
